﻿using Microsoft.Extensions.Logging;
using ST.Common.Util.Logging;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;

namespace ST.Common.Util.Drawing
{
	public class EAN13CodeHelper
	{
		public static ILogger logger = LoggingHelper.CreateLogger<EAN13CodeHelper>();

		/// <summary>
		/// 校验编码是否正确
		/// </summary>
		/// <param name="code"></param>
		/// <returns></returns>
		public static bool Check(string code)
		{
			if (string.IsNullOrEmpty(code) || code.Length != 13)
			{
				return false;
			}
			return GetCheckCode(code) == code[12].ToString();
		}

		/// <summary>
		/// 获取校验码
		/// </summary>
		/// <param name="code"></param>
		/// <returns></returns>
		public static string GetCheckCode(string code)
		{
			if (string.IsNullOrEmpty(code) || code.Length < 12)
			{
				return "";
			}
			int sumOdd = 0;
			int sumEven = 0;
			try
			{
				int result;
				for (int i = 0; i < 12; i++)
				{
					if (i % 2 == 0)
					{
						sumOdd += Convert.ToInt32(code.Substring(i, 1));
					}
					else
					{
						sumEven += Convert.ToInt32(code.Substring(i, 1));
					}
				}
				sumEven = sumEven * 3;
				result = 10 - (sumOdd + sumEven) % 10;
				result = result == 10 ? 0 : result;
				return result.ToString();
			}
			catch (Exception ex)
			{
				logger.LogError(ex, ex.Message);
			}
			return "";
		}

		public static Bitmap GenEAN13Code(string code, string text, int width, int height)
		{
			Bitmap bmp = null;
			try
			{
				//MultiFormatWriter mutiWriter = new MultiFormatWriter();
				////Dictionary<EncodeHintType, object> hint = new Dictionary<EncodeHintType, object>();
				////hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
				////hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);

				////生成条形码
				////BitMatrix bm = mutiWriter.encode(code, BarcodeFormat.EAN_13, width, height,hint);
				//BitMatrix bm = mutiWriter.encode(code, BarcodeFormat.EAN_13, width, height - 15, null);
				//BarcodeWriter barcodeWriter = new BarcodeWriter();
				////barcodeWriter.Format = BarcodeFormat.EAN_13;
				//Bitmap bitmap = barcodeWriter.Write(bm);

				////将img转换成bmp格式，否则后面无法创建 Graphics对象
				//Font font = new Font(FontFamily.GenericSansSerif, 18);
				//Brush brush = new SolidBrush(Color.Black);
				//Bitmap bmp = new Bitmap(bitmap.Width + 20, bitmap.Height + 15, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
				//using (Graphics g = Graphics.FromImage(bmp))
				//{
				//	g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
				//	g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
				//	g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
				//	g.DrawImage(bitmap, 20, 0);


				//	//白底
				//	//g.FillRectangle(Brushes.White, 0, 0, width, height - 15);
				//	g.FillRectangle(Brushes.White, 0, height - 40, 20, 25);
				//	g.DrawString(code.Substring(0, 1), font, brush, 0, height - 40);
				//	g.FillRectangle(Brushes.White, 32, height - 40, width / 2 - 17, 25);
				//	g.DrawString(code.Substring(1, 6), font, brush, 32, height - 40);
				//	g.FillRectangle(Brushes.White, width / 2 + 23, height - 40, width / 2 - 15, 25);
				//	g.DrawString(code.Substring(7, 6), font, brush, width / 2 + 23, height - 40);
				//	Font fontLabel = new Font(FontFamily.GenericSansSerif, 8);
				//	SizeF size = g.MeasureString(text, fontLabel);
				//	g.FillRectangle(Brushes.White, 0, height - 15, width + 20, 18);
				//	g.DrawString(text, fontLabel, brush, (width + 20 - size.Width) / 2, height - 15);
				//}
			}
			catch (Exception ex)
			{
				logger.LogError(ex, ex.Message);
			}
			return bmp;
		}
	}
}
